//
//  Usermessage.m
//  TengyunStation
//
//  Created by Johnny on 15/8/25.
//  Copyright (c) 2015年 xingrong. All rights reserved.
//

#import "Usermessage.h"
#import "SocketMethod.h"
#import "TSHeader.h"

@implementation Usermessage

- (id)initWithData:(NSData *)data Telephone:(NSString *)telephone {
    self = [super init];
    if (self) {
        self.telephone = telephone;
        int position = 0;
        NSInteger userLength = [SocketMethod integerSubdata:data Range:NSMakeRange(position, 4)];
        position += 4;
        NSString *userString = [SocketMethod stringSubdata:data Range:NSMakeRange(position, userLength)];
        [self handleUserString:userString];
        position += userLength;
        NSInteger websiteLength = [SocketMethod integerSubdata:data Range:NSMakeRange(position, 4)];
        position += 4;
        NSString *websiteString = [SocketMethod stringSubdata:data Range:NSMakeRange(position, websiteLength)];
        [self handleWebsiteString:websiteString];
    }
    return self;
}

- (void)handleUserString:(NSString *)userString {
    DLog(@"%@",userString);
    if (userString.length > 0) {
        NSArray *array = [userString componentsSeparatedByString:@","];
        self.userId = [SocketMethod subArray:array Index:0];
        self.userName = [SocketMethod subArray:array Index:1];
        self.sex = [[SocketMethod subArray:array Index:2] intValue];
        self.allWithdraw = [[SocketMethod subArray:array Index:3] floatValue];
        self.personalGrade = [[SocketMethod subArray:array Index:4] floatValue];
        self.registerTime = [SocketMethod subArray:array Index:5];
        self.frozenMoney = [[SocketMethod subArray:array Index:6] floatValue];
        self.margin = [[SocketMethod subArray:array Index:7] floatValue];
        self.pwdUpdateTime = [SocketMethod subArray:array Index:8];
        self.surplusMoney = self.allWithdraw + self.frozenMoney;
    }
}

- (void)handleWebsiteString:(NSString *)websiteString {
    DLog(@"%@",websiteString);
    if (websiteString.length > 0) {
        NSArray *array = [websiteString componentsSeparatedByString:@","];
        self.websiteId = [SocketMethod subArray:array Index:0];
        self.websiteName = [SocketMethod subArray:array Index:1];
        self.websiteQcode = [SocketMethod subArray:array Index:2];
        self.websiteCode = [SocketMethod subArray:array Index:3];
        self.websiteIsSend = [[SocketMethod subArray:array Index:4] boolValue];
        self.websiteGrade = [[SocketMethod subArray:array Index:5] floatValue];
        self.zitiIsSend = [[SocketMethod subArray:array Index:6] boolValue];
    }
}

- (void)setRegisterTime:(NSString *)registerTime {
    NSString *time = [SocketMethod dateStringWithDateString:registerTime];
    if (time.length > 10) {
        _registerTime = [time substringToIndex:10];
    }
}

- (void)usermessageSaveOrUpdate {
    Usermessage *usermessage = [Usermessage findFirstByCriteria:[NSString stringWithFormat:@"WHERE userId = '%@'",self.userId]];
    if (usermessage == nil) {
        [super save];
    } else {
        self.pk = usermessage.pk;
        [self update];
    }
}

@end
